package www.cskaoyan.com.controller;

import www.cskaoyan.com.bean.Account;
import www.cskaoyan.com.service.LoginService;
import www.cskaoyan.com.service.impl.LoginServiceImpl;
import www.cskaoyan.com.utils.Request;
import www.cskaoyan.com.utils.Response;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class LoginController {

    // 调用逻辑层
    LoginService loginService;
    public LoginController(){
        this.loginService = new LoginServiceImpl();
    }

    /**
     * 检查账号是否存在
     * @param request: Request{url='Login/chackAccount', methods='POST', parameters={userAccount=admin}}
     * @return Response(200, "成功", true)
     */
    public Response checkAccount(Request request)  {
        System.out.println("checkAccount");
        System.out.println(request);
        Response response;
        System.out.println("--------------ssssss---------------");
        Account account = new Account(request.getParameters());
        Integer userId = null;
        try {
            userId = loginService.checkAccount(account.getUserAccount());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (userId != null){
            // 存在
            response = new Response(200, "账号存在", true);
            response.setData(userId);
        }else {
            // 不存在
            response = new Response(0, "账号不存在", true);
        }
        return  response;
    }

    /**
     * 密码检查是否正确
     * @param request: Request{url='Login/chackPassword', methods='POST', parameters={userPassWord=123456, userAccount=admin}}
     * @return: Response
     */
    public Response chackPassword(Request request){
        System.out.println("chackPassword");
        System.out.println(request);
        Response response;
        Account account = new Account(request.getParameters());
        Integer userId = null;
        try {
            userId = loginService.chackPassword(account);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (userId != null){
            // 存在
            response = new Response(200, "账号存在", true);
            response.setData(userId);
        }else {
            // 不存在
            response = new Response(0, "账号不存在", true);
        }
        return  response;
    }

    /**
     * 根据账号密码登录
     * @param request: Request{url='Login/login', methods='POST', parameters={userPassWord=123456, userAccount=admin}}
     * @return: Response(200, "成功", true)
     */
    public Response login(Request request){
        System.out.println("login");
        System.out.println(request);
        Response response;
        Account account = new Account(request.getParameters());
        Integer userId = null;
        try {
            userId = loginService.chackPassword(account);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (userId != null){
            // 存在
            response = new Response(200, "账号存在", true);
            account.setUserId(userId);
            response.setData(account);
        }else {
            // 不存在
            response = new Response(0, "账号不存在", true);
        }
        return  response;
    }

}
